from django.shortcuts import render, HttpResponse, redirect, reverse
# import json
from django.http import JsonResponse
from django import views
from app01 import models


# Create your views here.
def login(request):
    if request.method == 'POST':
        name = request.POST.get('username')
        pwd = request.POST.get('password')
        # 查询数据库 完成数据校验   select * from user where name=xx and pwd=xx
        # res = models.User.objects.filter(name=name, pwd=pwd)
        # 插入数据
        # models.User.objects.create(name=name, pwd=pwd)
        # 更新数据
        # models.User.objects.filter(uid=4).update(name='heheheheeh')  # 默认更改所有数据
        # 删除数据
        # models.User.objects.filter(uid=4).delete()   # 默认全部删除
        # user_obj = res.first()  # 获取queryset 第一个数据对象
        # if user_obj:
        #     print(user_obj.uid)
        #     print(user_obj.name)
        #     print(user_obj.pwd)
        # else:
        #     print('用户名或密码错误')
        # print(name)
        # print(pwd)
        # print(res)
    return render(request, 'login.html')


def test(request, aaa):
    print(aaa)
    return HttpResponse('test')


def testadd(request):
    return HttpResponse('testadd')


# def home(request):
#     print(reverse('index_view', args=(1,)))  # reverse 方法提供方向解析
#     return render(request, 'home.html')


def index(request, *args, **kwargs):
    return HttpResponse('index')


def func(request):
    print(reverse('app01:func_view'))  # 多个app并且路由中的别名冲突、使用反向解析不能自动设别
    return HttpResponse('from app01 func')


def func1(request):
    # print('fom func1')
    # user_dict = {'name': 'ckk呵呵', 'pwd': 123}
    # return HttpResponse(json.dumps(user_dict,ensure_ascii=False))
    # return JsonResponse(user_dict, json_dumps_params={'ensure_ascii': False})
    lll = [1, 22, 33, 44, 55]
    return JsonResponse(lll, safe=False)


def get_file(request):
    if request.method == 'POST':
        username = request.POST.get("username")
        file_obj = request.FILES.get("file")
        print(username)
        print(file_obj)  # 文件对象
        with open(file_obj.name, 'wb') as f:
            for line in file_obj:
                f.write(line)

    return render(request, 'file.html')


class MyClass(views.View):
    def get(self, requset):
        return HttpResponse('from CBV get function')

    def post(self, requset):
        return HttpResponse('from CBV post function')


# 图书管理系统

def bms_home(request):
    return render(request, 'bms_home.html')


def bms_book_list(request):
    # 查询所有的书籍数据
    book_query = models.Book.objects.all()
    return render(request, 'bms_book_list.html', locals())


def bms_book_add(request):
    # if request.method == 'POST':
    #     # 获取新书籍相关的数据
    #     title = request.POST.get('title')
    #     price = request.POST.get('price')
    #     publish_time = request.POST.get('publish_time')
    #     publish_id = request.POST.get('publish_id')
    #     author_list = request.POST.get('author_list')
    #     # 小的校验全部忽略 直接写入数据
    #     book_obj = models.Book.objects.create(title=title, price=price, publish_time=publish_time,
    #                                           publish_id=publish_id)
    #     book_obj.authors.add(*author_list)
    #     # 跳转到书籍展示页展示数据
    #     return redirect('app01:app01_book_list_view')
    #
    # # 查询出版社和作者信息
    # publish_queryset = models.Publish.objects.all()
    # author_queryset = models.Author.objects.all()
    # return render(request, 'bms_book_add.html', locals())
    if request.method == 'POST':
        # 获取新书籍相关的数据
        title = request.POST.get('title')
        price = request.POST.get('price')
        publish_time = request.POST.get('publish_time')
        publish_id = request.POST.get('publish_id')
        author_list = request.POST.getlist('author_list')  # [1,2,3]
        # 小的校验我们这里就全部忽略 直接写入数据
        book_obj = models.Book.objects.create(title=title, price=price, publish_time=publish_time,
                                              publish_id=publish_id
                                              )
        book_obj.authors.add(*author_list)  # add(*[1,2,3])  add(1,2,3)
        # 跳转到书籍展示页查看数据
        return redirect('app01:app01_book_list_view')

    # 查询出版社和作者信息
    publish_queryset = models.Publish.objects.all()
    author_queryset = models.Author.objects.all()
    return render(request, 'bms_book_add.html', locals())



def bms_book_edit(request, edit_id):
    # 根据edit_id获取想要编辑的数据对象
    edit_obj = models.Book.objects.filter(pk=edit_id).first()
    if request.method == 'POST':
        # 获取新书籍相关的数据
        title = request.POST.get('title')
        price = request.POST.get('price')
        publish_time = request.POST.get('publish_time')
        publish_id = request.POST.get('publish_id')
        author_list = request.POST.get('author_list')
        models.Book.objects.filter(pk=edit_id).update(title=title, price=price, publish_time=publish_time,
                                                      publish_id=publish_id)
        edit_obj.authors.set(author_list)
        return redirect('app01:app01_book_list_view')

        # 查询出版社和作者信息
    publish_queryset = models.Publish.objects.all()
    author_queryset = models.Author.objects.all()
    return render(request, 'bms_book_edit.html', locals())

def bms_book_delete(request, delete_id):
    models.Book.objects.filter(pk=delete_id).delete()
    return redirect('app01:app01_book_list_view')

def ab_ajax(request):
    if request.method == 'POST':
        print(request.POST) # 普通数据
        print(request.FILES) # 文件数据
        # print(request.POST)
        # print(request.body)
        # i1 = request.POST.get('i1')
        # i2 = request.POST.get('i2')
        # res = int(i1) + int(i2)
        # return HttpResponse(res)
        # data_json = request.body
        # import json
        # data_dict = json.loads(data_json)  # 自带解码
        # print(data_dict, type(data_dict))
        return redirect('https://www.baidu.com')  # 不会跳转
    return render(request, 'ab_ajax.html')

# from app02 import models
# def ab_dk(request):
#     # 1.app02book表中中插入十万数据
#     # for i in range(1000000):
#     #     models.App02Book.objects.create(title=f'第{i}本书')
#     obj_list = []
#     for i in range(100000):
#         class_obj = models.App02Book(title=f'第{i}本书')
#         obj_list.append(class_obj)
#     models.App02Book.objects.bulk_create(obj_list)  # 批量创建
#     # models.App02Book.objects.bulk_update(obj_list)  # 批量更新数据
#     # 2.再查询出所有的数据展示给用户查看
#     book_queryset = models.App02Book.objects.all()
#     return render(request, 'ab_bk.html', locals())